package ufc.qx.si.item2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ufc.qx.si.Despesa;

public class SAXUtil extends DefaultHandler {

	private static final File FILE =  new File("saida.xml");
	private static final File FileBin = new File("filebin");
	private List<Despesa> despesas;
	private Despesa despesa;
	private String nomeTag;
	private StringBuffer buffer = new StringBuffer(100);

	public void parseDocument() {
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
			SAXParser sp = spf.newSAXParser();
			sp.parse(FILE, this);
		}catch(SAXException se) {
			se.printStackTrace();
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (IOException ie) {
			ie.printStackTrace();
		}
	}


	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		despesas = new ArrayList<Despesa>();
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
							 Attributes attributes) throws SAXException {
		if(qName.equalsIgnoreCase("despesa")){
			despesa = new Despesa();
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		buffer.append(ch, start, length);
	}
	
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		
		if(qName.equalsIgnoreCase("despesa")){
			this.despesas.add(this.despesa);
		} else if(qName.equalsIgnoreCase("id")){
			this.despesa.setId(Long.parseLong(this.buffer.toString().trim()));
		} else if(qName.equalsIgnoreCase("siglaUnidadeFederativa;")){
			this.despesa.setSiglaUnidadeFederativa(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("siglaPartido")){
			this.despesa.setSiglaPartido(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("descricaoCargo")){
			this.despesa.setDescricaoCampo(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("nomeCandidato")){
			this.despesa.setNomeCandidato(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("numeroCandidato")){
			this.despesa.setNumeroCandidato(Integer.parseInt(this.buffer.toString().trim()));
		} else if(qName.equalsIgnoreCase("data")){
			this.despesa.setData(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("documentoFornecedor")){
			this.despesa.setDocumentoFornecedor(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("siglaUnidadeFederativaFornecedor")){
			this.despesa.setSiglaUnidadeFederativaFornecedor(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("nomeFornecedor")){
			this.despesa.setNomeFornecedor(this.buffer.toString().trim());
		} else if(qName.equalsIgnoreCase("valor")){
			this.despesa.setValor(Double.parseDouble(this.buffer.toString().trim()));
		} else if(qName.equalsIgnoreCase("tipoDespesa")){
			this.despesa.setTipoDespesa(this.buffer.toString().trim());
		}
		
		buffer.delete(0, buffer.length());

	}

	public void salvarListaBinario() throws FileNotFoundException, IOException{
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FileBin));
		out.writeObject(despesas);
		out.close();
	}
	
	public void printElements(){
		for(Despesa d : despesas){
			System.out.println(d.toString());
		}
	}

	public List<Despesa> getDespesas() {
		return despesas;
	}


	public void setDespesas(List<Despesa> despesas) {
		this.despesas = despesas;
	}


	public Despesa getDespesa() {
		return despesa;
	}


	public void setDespesa(Despesa despesa) {
		this.despesa = despesa;
	}


	public String getNomeTag() {
		return nomeTag;
	}


	public void setNomeTag(String nomeTag) {
		this.nomeTag = nomeTag;
	}
	

}
